package br.uneb.tebd.local;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.uneb.tebd.R;
import br.uneb.tebd.model.Guitar;
import br.uneb.tebd.model.GuitarRepository;

public class SelectLocalGuitarByDescription extends Activity {
	
	private GuitarRepository guitarRepository;
	private Guitar guitar;
	private Button searchButton;
	private TextView description;
	private ListView listView;
	private ListAdapter list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.find_guitar);
		
		this.guitarRepository = new GuitarRepository(this);
		
		this.searchButton = (Button) findViewById(R.id.searchButton);

		this.listView = (ListView) findViewById(R.id.listView);
		
		this.description = (TextView) findViewById(R.id.descriptionSearch);
		
		this.searchButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				String guitarDescription = description.getText().toString().trim();
				
				if (guitarDescription.equals("") == false) {
					guitar = guitarRepository.findGuitarByDescription(guitarDescription);
				}
				
				if (guitar != null) {
					list = new LocalGuitarListAdapter(getApplicationContext(), guitar);
					listView.setAdapter(list);
				}
				
			}
			
		});
	}
}
